;;; cutecursor.el --- Change cursor smartly
 
;; Copyright (C) 2015 Octagram Sun
 
;; Author: Octagram Sun <octagram@qq.com>
 
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
 
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
 
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
 
;;; Commentary:
;;
;; Change cursor smartly
 
;;; Installation:
;;
;; Copy cutecursor.el to your load-path and add following statement to
;; your .emacs:
;;
;; (require 'cutecursor)
;;
;; then M-x cutecursor-mode to use this library.
 
;;; History:
;;
;; 2015-03-05
;;      * initial version 1.0.
 
;;; Code:
 
(require 'cursor-chg)
 
(defgroup cutecursor nil
  "Group for cutecursor."
  :group 'cursor
  :prefix "cutecursor-")
 
(defvar cutecursor-old-cursor-type nil "Cursor type before toggle on function `cutecursor-mode'.")
(defvar cutecursor-old-blink-cursor-mode nil "`blink-cursor-mode' before toggle on function `crsor-change-mode'.")
 
(setq curchg-default-cursor-color "deep sky blue")
 
;;;###autoload
(define-minor-mode cutecursor-mode
  "Toggle `cutecursor-mode'."
  :global t
  :group 'cutecursor
  (if cutecursor-mode
      (progn
        (setq cutecursor-old-cursor-type (cutecursor-get-cursor-type))
        (setq cutecursor-old-blink-cursor-mode blink-cursor-mode))
    (blink-cursor-mode (if cutecursor-old-blink-cursor-mode 1 -1)))
  (toggle-cursor-type-when-idle cutecursor-mode)
  (change-cursor-mode cutecursor-mode)
  (unless cutecursor-mode
    (curchg-set-cursor-type cutecursor-old-cursor-type)))
 
(defun cutecursor-get-cursor-type (&optional frame)
  "Get cursor type of frame FRAME."
  (cdr (assq 'cursor-type (frame-parameters frame))))
 
(defun curchg-change-cursor-on-overwrite/read-only ()
  "Set cursor type differently for overwrite mode, function `view-mode', and read-only buffer.
That is, use one cursor type for overwrite mode and read-only buffers,
and another cursor type otherwise."
  (curchg-set-cursor-type
   (if (or buffer-read-only overwrite-mode view-mode)
       curchg-overwrite/read-only-cursor-type
     curchg-default-cursor-type)))
 
(provide 'cutecursor)
 
;;; cutecursor.el ends here
